<?php
set_time_limit(300);
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
//定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML',true);

pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format','',0);

/**
 * 轮询管理
 */
class poll extends admin
{
	private $poll,$pollinfo;
	public $siteid;

	public function __construct()
	{
		parent::__construct();
		$this->poll = pc_base::load_model('polls_model');
		$this->pollinfo = pc_base::load_model('polls_info_model');
		$this->siteid = get_siteid();
	}

	/**
	 * 轮询列表
	 */
	public function index ()
	{
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = ' status = 1 AND siteid = ' . $this->siteid;
		if ($_GET['name']) {
			$name = $_GET['name'];
			$where .= " AND `title` LIKE '%$name%' OR  `varname` LIKE '%$name%' ";		
		}
		$poll_list = $this->poll->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->poll->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('poll_list');
	}

	/**
	 * 添加轮询
	 */
	public function add ()
	{
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$poll = $this->check($_POST['poll']);
			$id = $this->poll->insert($poll, true);
			if ($id) {
				//记录行为日志
                $log_content = '<span style="color:green;">添加轮询</span>[新生成轮询id：'.$id.'；轮询标题：'.$poll['title'].']';
                $this->manage_admin_log($log_content);
				showmessage(L('add_poll_success'), '?m=poll&c=poll&a=index');
			} else {
				showmessage(L('add_poll_error'), HTTP_REFERER);
			}	
		} else {
			//获取站点模板信息
			$show_dialog = $show_validator = true;
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('poll_add');
		}
	}

	/**
	 * 轮询修改
	 */
	public function edit (){
		if (!isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = intval($_GET['id']);
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$poll = $this->check($_POST['poll'], 'edit');
			$id = $this->poll->update($poll, array('id'=>$id, 'siteid'=>$this->siteid));
			if ($id) {
				//记录行为日志
                $log_content = '<span style="color:blue;">修改轮询</span>[轮询id：'.$id.'；轮询标题：'.$poll['title'].']';
                $this->manage_admin_log($log_content);
				showmessage(L('edit_poll_success'), '?m=poll&c=poll&a=index');
			} else {
				showmessage(L('edit_poll_error'), HTTP_REFERER);
			}	
		} else {
			$show_dialog = $show_validator = true;
			$info = $this->poll->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
			extract($info);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('poll_edit');
		}
	}

	/**
	 * 轮询删除
	 */
	public function delete ()
	{
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['id'])){
				foreach($_POST['id'] as $jobid_arr) {
					$this->poll->update(array('status'=>'-1'),array('id'=>$jobid_arr));
				}

				//记录行为日志
	            $id = implode(',',$_POST['id']);
	            $log_content = '<span style="color:red;">批量删除轮询</span>[轮询id：('.$id.')]';
	            $this->manage_admin_log($log_content);

				showmessage(L('operation_success'),HTTP_REFERER);
			}else{
				$id = intval($_GET['id']);
				$title = $_GET['title']?$_GET['title']:'';
				if($id < 1) return false;
				$result = $this->poll->update(array('status'=>'-1'),array('id'=>$id));
				if($result){

					//记录行为日志
		            $log_content = '<span style="color:red;">删除轮询</span>[轮询id：'.$id.'；标题：'.$title.']';
		            $this->manage_admin_log($log_content);

					showmessage(L('operation_success'),HTTP_REFERER);
				}else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
		}
	}

	/**
	 * 检验变量名是否存在，避免导师重复
	 */
	public function public_check_poll ()
	{
		if(!$_GET['varname']) {echo 0;die;}
		if(pc_base::load_config('system', 'charset')=='gbk') {
			$_GET['varname'] = safe_replace(iconv('UTF-8', 'GBK', $_GET['varname']));
		}
		if($_GET['id']) {
			$id = intval($_GET['id']);
			$r = $this->poll->get_one(array('id'=>$id, 'siteid' => $this->siteid));
			if($r['varname'] == $_GET['varname']) {
				echo 1;die;
			}
		}
		$r = $this->poll->get_one(array('siteid' => $this->siteid, 'varname' => $_GET['varname']), 'id');
		if($r['id']) {
			echo 0;die;
		} else {
			echo 1;die;
		}
	}

	/**
	 * 轮询信息列表
	 */
	public function pollList ()
	{
		$pollid = $_GET['pollid']?(int)$_GET['pollid']:0;
		if (!$pollid) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$where = array();
		$where['siteid'] = $this->siteid;
		$where['status'] = 1;
		$where['pollid'] = $pollid;
		$page = $_GET['page']?(int)$_GET['page']:1;
		$poll_list = $this->pollinfo->listinfo($where, 'istop DESC,myorder DESC,id DESC', $page, 20);
		$pages = $this->poll->pages;
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=poll&c=poll&a=addInfo&pollid='.$pollid.'\', title:\''.L('add_poll_info').'\', width:\'700\', height:\'350\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_poll_info'));
		include $this->admin_tpl('info_list');
	}

	/**
	 * 添加轮询信息内容
	 */
	public function addInfo ()
	{
		$pollid = $_GET['pollid']?(int)$_GET['pollid']:0;
		if (!$pollid) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		//给pollid为6的轮询加提示语
		if ($pollid==6) {
		    $imgReminder = "为保证前台显示效果，请上传宽高为500*700的信息图片！";
		}
		//给pollid为5的轮询加提示语
		if ($pollid==5) {
		    $imgReminder = "为保证前台显示效果，请上传宽高为560*370的信息图片！";
		}
		//给pollid为8-15的轮询加提示语
		if ($pollid==8 || $pollid==9 || $pollid==10 || $pollid==11 || $pollid==12 || $pollid==13 || $pollid==14 || $pollid==15) {
		    $imgReminder = "APP版本通屏轮播图，最小尺寸为1000*550，可等比例放大！";
		}
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$info = $_POST['info'];
			$info['myorder'] = (int)$info['myorder'];
			$info['siteid'] = $this->siteid;
			$info['addtime'] = date('Y-m-d H:i:s');
			$info['addusername'] = param::get_cookie('admin_username');
			$info['adduserid'] = $_SESSION['userid'];
			$info['pollid'] = $pollid;
			$id = $this->pollinfo->insert($info, true);
			if ($id) {
				$pollInfo = $this->poll->get_one(array('id'=>$pollid));
				//记录行为日志
                $log_content = '<span style="color:green;">添加轮询信息</span>[轮询id：'.$pollid.',轮询标题：'.$pollInfo['title'].'。新生成信息id：'.$id.'；信息标题：'.$info['title'].']';
                $this->manage_admin_log($log_content);

				showmessage(L('add_info_success'), HTTP_REFERER, '', 'add');
			} else {
				showmessage(L('add_info_error'), HTTP_REFERER);
			}	
		} else {
			//获取站点模板信息
			$show_dialog = $show_validator = true;
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('info_add');
		}
	}

	/**
	 * 修改轮询信息内容
	 */
	public function editInfo ()
	{
		if (!isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = intval($_GET['id']);
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$infoInfo = $this->pollinfo->get_one(array('id'=>$id));
			$info = $_POST['info'];
			$info['myorder'] = (int)$info['myorder'];
			$info['updatetime'] = date('Y-m-d H:i:s');
			$id = $this->pollinfo->update($info, array('id'=>$id, 'siteid'=>$this->siteid));
			if ($id) {
				//取轮询信息
				$pollInfo = $this->poll->get_one(array('id'=>$infoInfo['pollid']));
				//记录行为日志
                $log_content = '<span style="color:blue;">修改轮询信息</span>[轮询id：'.$infoInfo['pollid'].'，轮询标题：'.$pollInfo['title'].'。信息id：'.$_GET['id'].'；信息标题：'.$info['title'].']';
                $this->manage_admin_log($log_content);

				showmessage(L('edit_info_success'), HTTP_REFERER, '', 'edit');
			} else {
				showmessage(L('edit_info_error'), HTTP_REFERER);
			}	
		} else {
			$show_dialog = $show_validator = true;
			$info = $this->pollinfo->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
			//给pollid为6的轮询加提示语
    		if ($info["pollid"]==6) {
    		    $imgReminder = "为保证前台显示效果，请上传宽高为500*700的信息图片！";
    		}
    		//给pollid为5的轮询加提示语
    		if ($info["pollid"]==5) {
    		    $imgReminder = "为保证前台显示效果，请上传宽高为560*370的信息图片！";
    		}
    		//给pollid为8-15的轮询加提示语
    		if ($info["pollid"]==8 || $info["pollid"]==9 || $info["pollid"]==10 || $info["pollid"]==11 || $info["pollid"]==12 || $info["pollid"]==13 || $info["pollid"]==14 || $info["pollid"]==15) {
    		    $imgReminder = "APP版本通屏轮播图，最小尺寸为1000*550，可等比例放大！";
    		}
			extract($info);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('info_edit');
		}
	}

	/**
	 * 删除轮询信息内容
	 */
	public function delInfo ()
	{
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['id'])){
				foreach($_POST['id'] as $jobid_arr) {
					$this->pollinfo->delete(array('id'=>$jobid_arr));
				}

				//记录行为日志
	            $id = implode(',',$_POST['id']);
	            $log_content = '<span style="color:red;">批量删除轮询信息</span>[信息id：('.$id.')]';
	            $this->manage_admin_log($log_content);

				showmessage(L('operation_success'),HTTP_REFERER);
			}else{
				$id = intval($_GET['id']);
				$title = $_GET['title']?$_GET['title']:'';
				if($id < 1) return false;
				$result = $this->pollinfo->delete(array('id'=>$id));
				if($result){
					//记录行为日志
		            $log_content = '<span style="color:red;">删除轮询信息</span>[信息id：'.$id.'；'.$title.']';
		            $this->manage_admin_log($log_content);

					showmessage(L('operation_success'),HTTP_REFERER);
				}else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
		}
	}

	/**
	 * 表单验证
	 * @param array $data 表单传递的值
	 * @param string $a add/edit添加操作时，自动加上默认值
	 */
	private function check($data, $a = 'add') {
		if(!$data['varname']) showmessage(L('poll_varname').L('no_empty'), HTTP_REFERER);
		if(!$data['title']) showmessage(L('poll_title').L('no_empty'), HTTP_REFERER);
		if($a=='add') {
			$data['siteid'] = $this->siteid;
			$data['addtime'] = date('Y-m-d H:i:s');
			$data['addusername'] = param::get_cookie('admin_username');
			$data['adduserid'] = $_SESSION['userid'];
		} else {
			$data['updatetime'] = date('Y-m-d H:i:s');
		}
		return $data;
	}

	
}